package tool.soft

import java.io.File
import dao._
import implicits.Implicits._
import models.Tables
import models.Tables._
import org.joda.time.DateTime
import play.api.data.FormBinding
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool.plot._
import tool.soft.AllSoft.SoftBT
import tool._
import play.api.i18n.Messages
import shared.pojo.mission._
import upickle.default._
import utils.Utils

/** Created by yz on 15/9/2020
  */
object ViolinPlotSoft extends ViolinPlotSoftT with SoftBT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    List(
      FileInfoData("AllMet_Test.csv", messages("allMetTest")),
      FileInfoData("AllMet_Vioplot.pdf", messages("allMetVioplot"))
    )
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit
      request: Request[AnyContent],
      formBinding: FormBinding
  ) = {
    Utils.t2ConfigFileWithDefault(workspaceDir, data)
    plot.ViolinPlot.initConfigFile(workspaceDir, idStr = "")
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.violinPlotResult(missionId)
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    val groupNum = Tool.getGroupNum(userId, data.groupFile)
    val isPairedStr = if (data.isPaired) messages("yes") else messages("no")
    if (groupNum == 2) {
      ujson.Obj(messages("analysisMethod") -> Tool.getMethod(data.method), messages("paired") -> isPairedStr)
    } else {
      ujson.Obj(messages("analysisMethod") -> Tool.getMulMethod(data.method))
    }
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val isMul = Tool.isMulGroup(workspaceDir)
    val testCommand = if (isMul) {
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "mul_group_p_value_calculate.R").unixPath}
         |""".stripMargin
    } else {
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "p_value_calculate.R").unixPath}
         |""".stripMargin
    }
    val command =
      s"""
         |${Tool.rScript} ${Tool.rPath.unixPath}/produce_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath}
           """.stripMargin
    val fillCommand =
      s"""
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.ViolinPlot.plotO.configFileName}
         |$testCommand
                  """.stripMargin
    val plotCommand = plot.ViolinPlot.getCommand(workspaceDir, "")
    List(CommandData(workspaceDir, List(command, fillCommand, plotCommand)))
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit
      request: Request[AnyContent],
      messages: Messages
  ): Html = {
    views.html.user.soft.violinPlot(missionName, missionId)
  }

  override def getMission(implicit
      request: Request[AnyContent],
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages,
      formBinding: FormBinding
  ): Tables.MissionRow = {
    val initInputs = List(data.file, data.groupFile)
    val argJson = getArg
    MissionRow(
      id = 0,
      missionName = s"${data.missionName}",
      kind = nameStr,
      userId = userId,
      startTime = new DateTime(),
      state = "preparing",
      arg = argJson,
      input = initInputs
    )
  }

  override def produceInputFile(
      workspaceDir: File
  )(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawMetaboliteFile(userId, data.file, rawDataFile)
    val groupFile = new File(workspaceDir, "group.txt")
    Tool.produceRawGroupFile(userId, data.groupFile, rawDataFile, groupFile)
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    Tool.transformRq.transform(reader[ViolinPlotSoftData])
  }

}
